--
-- Author: jianbang.song
-- Date: 2018-9-6
--
-- BattleIconRender

local BattleIconRender = app.mvc.createRenderClass(...)

--data = 
function BattleIconRender.createWithData(data, node)
	local sprite = BattleIconRender.extend(node or cc.CSLoader:createNode("ui2/dynasty/Node_map_dynasty.csb"))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end     
function BattleIconRender.create(node)
    return BattleIconRender.createWithData(nil, node)
end 

-- data = pb_town.TownInfo
-- message TownInfo {
-- 	int32 id = 1;           // 城镇id
-- 	int32 prosperity = 2;   // 繁荣度
-- 	PHASE phase = 3; 		// 所处阶段
-- 	repeated Stronghold stronghold = 4; // 据点信息
-- 	int32 myBiddingPrice = 5; // 我的竞价（仅有权限的公会玩家才能查询，否则传0）
-- }

--  CLOSE = 0; // 未开放阶段
-- 	BIDDING = 1; // 竞价阶段
-- 	READY = 2; // 准备阶段
-- 	FIGHT = 3; // 开战阶段
-- 	END = 4; // 结束阶段
-- 	MUTABLE = 5; // 易变阶段（公会领地等级低于会长当前爵位所要求的等级，则该公会所有的领地都将变为每天可攻打状态）

function BattleIconRender:init(data)
	self.super.init(self, data)

	-- self._ui.button:addClickEventListener(function(sender, eventType)
	--     executeFunc(self._onClick, self)
    -- end)
    
    self.ani = self:findChild("Panel/ani_battle")

    self.battle = self:findChild("Panel/battle")
    self.gold = self:findChild("Panel/gold")
    self.blue = self:findChild("Panel/blue")
    self.red = self:findChild("Panel/red")
    self.map = self:findChild("Panel/map")
    self.name = self:findChild("Panel/name")

    self.icons = {self.map, self.red, self.blue, self.gold, self.battle}

    self.name:setString(L"")
    self.ani:hide()

    self:findChild("Panel"):addClickEventListener(function(sender, eventType)
        executeFunc(self._onTownClicked, self, self._data)
    end)

	self:refresh()
end

function BattleIconRender:refreshStatus(s)
    -- print("================== town status:", s)
    for i = 1, 5 do 
        self.icons[i]:hide()
    end
    self.ani:hide()

    if not self.icons[s] then
        s = 1
    end

    local bc = app:getInst("BiddingController")
    
    local function setByOwner()
        if bc:isMyTown(self._data.data.id) then
            self.blue:show()
        elseif bc:hasOwner(self._data.data.id) then
            self.red:show()
        else
            self.map:show()
        end
    end

    if s == 1 then --竞价阶段
        if self._data.data.myBiddingPrice > 0 then --如果本会会参与竞价
            self.gold:show()
        else
            setByOwner()
        end
        local _, istoday = self._data:getStartWeekDay()
        if istoday then
            if not self.fireAni then
                self.fireAni = self:createAni()
                self.fireAni:setPosition(30, 40)
                self:findChild("Panel"):addChild(self.fireAni, -1)
            end
        end
    elseif s == 2 then --准备
        self.battle:show()
    elseif s == 3 then --开战
        self.ani:show()
    else
        setByOwner()
    end
end

function BattleIconRender:createAni()
    local params = {
        path = Res.ArmatureEffectPath .. "firemap",
        armatureName = "firemap",
        animationName = "firemap",
        skeletonName = "firemap"
    }

    
    if not params then return end

    local Armature = app:getClass("Armature")
    local armature = Armature.create(params)
    armature:play(params.animationName, 10000) -- 循环播放暂时有bug，这里填一个比较大的次数

    -- self:getNodeParent():addChild(armature, 1002)
    return armature
end

function BattleIconRender:refresh()
	if self._data then
        self._ui.name:setString(self._data.config:getName())
        
        local s = protobuf.enum_id("pb_town.PHASE", self._data.data.phase)
        
        -- self._ui.name:setString(self._data.config:getName()..tostring(self._data.data.phase))
        
        self:refreshStatus(s)
	end
end

function BattleIconRender:setClickEnable(b)
    self._ui.button:setTouchEnabled(b)
    self._ui.button:setEnabled(b)
end

function BattleIconRender:onTownClicked(cb)
    self._onTownClicked = cb
end

function BattleIconRender:onBtnClick(cb)
    self._onClick = cb
end

return BattleIconRender